package com.cw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cw.entity.Staff;
import com.cw.mapper.StaffMapper;
import com.cw.service.StaffService;
import com.cw.utils.JwtUtils;
import com.cw.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: MySyetem
 * @description: 员工接口实现类
 * @author: 张庆恒
 * @create: 2021-08-27 20:12
 **/
@Service
public class StaffServiceImpl implements StaffService {
    @Resource
    private StaffMapper staffMapper;

    @Override
    public R getStaffByUP(String username, String password) {
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        wrapper.eq("staffid", username);
        wrapper.eq("password", password);
        Staff staff = staffMapper.selectOne(wrapper);
        if (staff != null) {
            Integer state = staff.getState();
            if (state == 0) {
                return new R(0, "您已离职无法登录！", 0, null);
            }
            if (state == 1) {
                String token = JwtUtils.createToken(staff.getId(), staff.getStaffname(), staff.getStaffid() + "", staff.getUrl(),staff.getRoleid());
                return new R(1, "登陆成功", 0, token);
            }
            if (state == 2) {
                return new R(2, "您的账户被冻结，请联系管理员解冻！", 0, null);
            }
        }
        return new R(3, "账号或密码错误！", 0, null);
    }

    @Override
    public R getAllStaff(Integer pageNow,Integer pageSize) {
        System.out.println(pageNow+"`````"+pageSize);
        IPage<Staff> iPage = new Page<>(pageNow, pageSize);
        IPage<Staff> page = staffMapper.selectPage(iPage,null);
        List<Staff> staff = page.getRecords();
        long total = page.getTotal();
        System.out.println(total);
        System.out.println(staff.size());
        return new R(1,"",total,staff);
    }

}
